<%-- 
    Document   : breadcrumbs
    Created on : 11-Jan-2013, 14:21:50
    Author     : Roald
--%>
<%@page import="nl.hva.web.mvcwebapp.doa.ForumDAO"%>
<%@page import="nl.hva.web.mvcwebapp.doa.NewsDAO"%>

<%
    //HTML mark up
    out.print("<div class='breadcrumbs'>");

    String url = request.getServletPath();
    String[] s = url.split("/");

    //Home
    out.println("<a href='" + request.getContextPath() + "'>" + "Home" + "</a>"); // Home

    //Second level breadcrumbs

    // Admin Management 
    if (request.getRequestURI().contains("admin")) {
        out.println(" > Admin Management"); // Courses

    } else //Course
    if (request.getRequestURI().contains("course")) {
        out.println(" > <a href='" + request.getContextPath() + "/course/catalog" + "'>" + "Courses" + "</a>"); // Courses
    } else //Forums Breadcrumbs
    if (request.getRequestURI().contains("forum")) {
        out.println(" > <a href='" + request.getContextPath() + "/forum" + "'>" + "Forum" + "</a>"); // Forums
        //See if there is an 'id=' in the request
        try {
            //Confirms the existance of an '=' in the url
            if (request.getQueryString().contains("=")) {
                //Found thread in the url query, for example 'thread_id=1'
                if (request.getQueryString().contains("thread")) {
                    String name = ForumDAO.getThread(Integer.parseInt(request.getQueryString().split("=")[1])).getForumThreadName();
                    out.println(">" + name);
                }
            }
        } catch (Exception E) {
        }
    } else //News Breadcrumbs
    if (request.getRequestURI().contains("news")) {
        out.println("> <a href='" + request.getContextPath() + "/news" + "'>" + "News" + "</a>"); // News
        try {
            if (request.getQueryString().contains("=")) {
                String name = NewsDAO.getNews(Integer.parseInt(request.getQueryString().split("=")[1])).getNewstitle();
                out.println("> " + name);
            }
        } catch (Exception E) {
        }
    } else // User Management
    if (request.getRequestURI().contains("user")) {
        out.println("> User Management");
    } else {
        //Last else, supposed to be there when everything else fails!
        for (int i = 0; i < s.length; i++) {
            if (!s[i].isEmpty()) {
                //Preformat is the unedited path
                String preformat = s[i];
                String format;
                //Defining chars to replace with
                char replaceMinus = '-';
                char replaceUnderscore = '_';
                char replaceSpace = ' ';

                //Changing preformat to format, removing all '-' and '_' for ' ' and removing .jsp in the end
                format = preformat.replace(replaceMinus, replaceSpace);
                format = format.replace(replaceUnderscore, replaceSpace);
                format = format.split(".jsp")[0];
                //Style tweak, changing the first letter to a capital
                format = String.valueOf(format.charAt(0)).toUpperCase() + format.substring(1);
                out.print(" > " + format);
            }
        }
    }
    //Closing off the HTML mark up
    out.print("<br class='clearfix'/></div><hr/>");
%>
